//
//  RDConfigStorage.h
//  RDelivery
//
//  Created by selmerzhang on 2021/8/11.
//  Copyright © 2021 Tencent. All rights reserved.
//

#ifndef RDConfigStorage_h
#define RDConfigStorage_h

#import "RDeliverySDKSettings.h"

@class RDConfigInfo, RDeliveryStorageUpdateInfo;
@protocol RDConfigInfoListener, RDConfigEventListener;

NS_ASSUME_NONNULL_BEGIN

@protocol RDConfigStorageDelegate <NSObject>

/// 存储完成初始化时回调
- (void)storageDidFinishInitial;

/// 监听配置改变事件
/// 在配置有更新时回调
/// @param keyArray 有更新的配置key的数组
- (void)onConfigChanged:(NSArray<NSString *> *)keyArray;

@end


@interface RDConfigStorage : NSObject

/// 拉取软限频间隔
@property(nonatomic, assign) uint32_t softPullInterval;

/// 拉取硬限频间隔
@property(nonatomic, assign) uint32_t hardPullInterval;

/// SDK上次发起请求的时间戳
@property(nonatomic, assign) NSTimeInterval lastPullTimestamp;

/// SDK上次请求的Json Body
@property (nonatomic, strong) NSString *lastPullJsonBody;

/// 根据settings初始化存储，存储路径由settings的storageId决定
- (instancetype)initWithSettings:(RDeliverySDKSettings *)settings delegate:(id<RDConfigStorageDelegate>)delegate;


/// 同步获取磁盘中所有配置项
- (NSDictionary<NSString *,RDConfigInfo *> *)syncGetConfigInfosFromDisk;

/// 获取内存中所有配置项
- (NSDictionary<NSString *,RDConfigInfo *> *)getConfigInfos;

/// 获取内存中单个配置
/// @param key 需要获取的配置的key
- (RDConfigInfo *)getConfigInfoForKey:(NSString *)key;

/// 删除内存、磁盘中单个配置
/// @param key 需要删除配置的key
- (void)deleteDataWithKey:(NSString *)key;

/// 批量获取内存中的配置
/// @param keys 需要获取的配置的key的集合
- (NSMutableArray<RDConfigInfo *> *)getConfigInfoArrayForKeys:(NSArray<NSString *> *)keys;

///同步读取磁盘中单个配置
/// @param key 需要获取的配置的key
- (RDConfigInfo *)getConfigInfoForKeyFromDisk:(NSString *)key;

/// 配置拉取时间（单位是毫秒）
/// @param key 配置 key
- (NSInteger)getLastRequestServerTimeForKey:(NSString *)key;

/// 全量配置拉取时间
- (NSInteger)getLastRequestServerTime;

/// 获取所有配置的key
- (NSArray<NSString *> *)allConfigKeys;


/// 更新存储
/// @param updateInfo 需要更新的配置信息
/// @param error 返回的错误
- (void)syncUpdateConfigs:(RDeliveryStorageUpdateInfo *)updateInfo
                    error:(NSError **)error;

/// 获取拉取配置请求的context
- (NSString *)getRequestContext;

//切换环境或userid时重置存储
- (void)resetStorageWithGuid:(NSString *)guid env:(NSString *)envId completedBlock:(void (^)(void))block;

/// 清理所有存储数据，包括内存、磁盘
- (void)clearAllCache;

#pragma mark - Listener
/// 为配置值变更添加一个监听者
/// @param listener 监听者
/// @param key 配置数据的key
- (void)addConfigInfoListener:(id<RDConfigInfoListener>)listener forKey:(NSString *)key;

/// 移除配置值变更监听者
/// @param listener 监听者
/// @param key 配置数据的key
- (void)removeConfigInfoListener:(id<RDConfigInfoListener>)listener forKey:(NSString *)key;


@end

NS_ASSUME_NONNULL_END

#endif /* RDConfigStorage_h */
